

class OpinionController extends SecureController{
    def authenticateService

    def topicService

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST', addOpinion:'POST']

    def addOpinion = {
        def user = session.user
        if(user) {
            def opinion = new Opinion(author:user, opinion:params?.opinion)
            def topic = Topic.get(params?.topicId)
            if(topic) {
                opinion.topic = topic

                try {
                    topic = topicService.addOpinion(topic, opinion)
                    flash.message = "添加观点成功"
                    
                }catch (TopicException te) {
                    flash.message = te.message
                }
            } else {
                flash.message = "添加观点失败${topic}"
            }
            redirect(controller:"topic",action:"show",id:params?.topicId)
        } else {
            redirect(controller:"login",action:"auth")
        }
    }

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ opinionInstanceList: Opinion.list( params ), opinionInstanceTotal: Opinion.count() ]
    }

    def show = {
        def opinionInstance = Opinion.get( params.id )

        if(!opinionInstance) {
            flash.message = "Opinion not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ opinionInstance : opinionInstance ] }
    }

    def delete = {
        def opinionInstance = Opinion.get( params.id )
        if(opinionInstance) {
            try {
                opinionInstance.delete(flush:true)
                flash.message = "Opinion ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Opinion ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Opinion not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def opinionInstance = Opinion.get( params.id )

        if(!opinionInstance) {
            flash.message = "Opinion not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ opinionInstance : opinionInstance ]
        }
    }

    def update = {
        def opinionInstance = Opinion.get( params.id )
        if(opinionInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(opinionInstance.version > version) {
                    
                    opinionInstance.errors.rejectValue("version", "opinion.optimistic.locking.failure", "Another user has updated this Opinion while you were editing.")
                    render(view:'edit',model:[opinionInstance:opinionInstance])
                    return
                }
            }
            opinionInstance.properties = params
            if(!opinionInstance.hasErrors() && opinionInstance.save()) {
                flash.message = "Opinion ${params.id} updated"
                redirect(action:show,id:opinionInstance.id)
            }
            else {
                render(view:'edit',model:[opinionInstance:opinionInstance])
            }
        }
        else {
            flash.message = "Opinion not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def opinionInstance = new Opinion()
        opinionInstance.properties = params
        return ['opinionInstance':opinionInstance]
    }

    def save = {
        def opinionInstance = new Opinion(params)
        if(!opinionInstance.hasErrors() && opinionInstance.save()) {
            flash.message = "Opinion ${opinionInstance.id} created"
            redirect(action:show,id:opinionInstance.id)
        }
        else {
            render(view:'create',model:[opinionInstance:opinionInstance])
        }
    }
}
